
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using Tathata.Common.Utils;
using Tathata.Common.BLL.EntityCollections;
using System.Collections.Generic;

namespace Tathata.Web.Administration
{
    public partial class Administration_CustomerRoleDetails : BaseNopAdministrationPage
    {
        private void BindData()
        {
            CustomerRole customerRole = CustomerRoleManager.GetByCustomerRoleID(this.CustomerRoleID);
            if (customerRole != null)
            {
                this.txtName.Text = customerRole.Name;
                this.cbFreeShipping.Checked = customerRole.FreeShipping;
                this.cbTaxExempt.Checked = customerRole.TaxExempt;
                this.cbActive.Checked = customerRole.Active;

                CustomerCollection customers = customerRole.Customers;
                gvCustomers.DataSource = customers;
                gvCustomers.DataBind();

                DiscountCollection discountCollection = customerRole.Discounts;
                List<int> _discountIDs = new List<int>();
                foreach (Discount dis in discountCollection)
                    _discountIDs.Add(dis.DiscountID);
                DiscountMappingControl.SelectedDiscountIDs = _discountIDs;
                DiscountMappingControl.BindData();
            }
            else
                Response.Redirect("CustomerRoles.aspx");
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.BindData();
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    CustomerRole customerRole = CustomerRoleManager.GetByCustomerRoleID(this.CustomerRoleID);

                    if (customerRole != null)
                    {
                        customerRole = CustomerRoleManager.UpdateCustomerRole(customerRole.CustomerRoleID,
                            txtName.Text, cbFreeShipping.Checked, cbTaxExempt.Checked, cbActive.Checked, customerRole.Deleted);

                        foreach (Discount discount in DiscountManager.GetDiscountsByCustomerRoleID(customerRole.CustomerRoleID))
                            DiscountManager.RemoveDiscountFromCustomerRole(customerRole.CustomerRoleID, discount.DiscountID);
                        foreach (int discountID in DiscountMappingControl.SelectedDiscountIDs)
                            DiscountManager.AddDiscountToCustomerRole(customerRole.CustomerRoleID, discountID);

                        Response.Redirect("CustomerRoleDetails.aspx?CustomerRoleID=" + customerRole.CustomerRoleID.ToString());
                    }
                    else
                        Response.Redirect("CustomerRoles.aspx");
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            try
            {
                CustomerRoleManager.MarkCustomerRoleAsDeleted(this.CustomerRoleID);
                Response.Redirect("CustomerRoles.aspx");
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }

        public int CustomerRoleID
        {
            get
            {
                return CommonHelper.QueryStringInt("CustomerRoleID");
            }
        }
    }
}
